/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.zcm.modules.center.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zcm.common.web.BaseController;
import com.zcm.modules.sys.service.SystemService;
import org.apache.http.util.TextUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zcm.common.config.Global;
import com.zcm.common.persistence.Page;
import com.zcm.common.web.BaseController;
import com.zcm.common.utils.StringUtils;
import com.zcm.modules.center.entity.CenterUser;
import com.zcm.modules.center.service.CenterUserService;

/**
 * 用户中心后台Controller
 * @author www
 * @version 2018-04-20
 */
@Controller
@RequestMapping(value = "${adminPath}/center/centerUser")
public class CenterUserController extends BaseController {

	@Autowired
	private CenterUserService centerUserService;
	
	@ModelAttribute
	public CenterUser get(@RequestParam(required=false) String id) {
		CenterUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = centerUserService.get(id);
		}
		if (entity == null){
			entity = new CenterUser();
		}
		return entity;
	}
	
	@RequiresPermissions("center:centerUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(CenterUser centerUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CenterUser> page = centerUserService.findPage(new Page<CenterUser>(request, response), centerUser); 
		model.addAttribute("page", page);
		return "modules/center/centerUserList";
	}

	@RequiresPermissions("center:centerUser:view")
	@RequestMapping(value = "form")
	public String form(CenterUser centerUser, Model model) {
		model.addAttribute("centerUser", centerUser);
		return "modules/center/centerUserForm";
	}

	@RequiresPermissions("center:centerUser:edit")
	@RequestMapping(value = "save")
	public String save(CenterUser centerUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, centerUser)){
			return form(centerUser, model);
		}
		if(TextUtils.isEmpty(centerUser.getId())){
			centerUser.setPassword(SystemService.entryptPassword(centerUser.getPassword()));
		}
		centerUserService.save(centerUser);
		addMessage(redirectAttributes, "保存用户成功");
		return "redirect:"+Global.getAdminPath()+"/center/centerUser/?repage";
	}
	
	@RequiresPermissions("center:centerUser:edit")
	@RequestMapping(value = "delete")
	public String delete(CenterUser centerUser, RedirectAttributes redirectAttributes) {
		centerUserService.delete(centerUser);
		addMessage(redirectAttributes, "删除用户成功");
		return "redirect:"+Global.getAdminPath()+"/center/centerUser/?repage";
	}

}